<?php

/**
 * This is the model class for table "{{cmp_promotion}}".
 *
 * The followings are the available columns in table '{{cmp_promotion}}':
 * @property integer $zcr_id
 * @property string $zcr_title
 * @property string $zcr_content
 * @property integer $zm_id
 * @property integer $zcr_start_time
 * @property integer $zcr_end_time
 * @property integer $zcr_type
 * @property string $zcr_condition
 * @property integer $zcr_status
 */
class CmpPromotion extends CActiveRecord
{
	//店铺名称，用于筛选
	public $company_name;
	
	//状态，用于筛选
	public $status;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return CmpPromotion the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{cmp_promotion}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zcr_title, zcr_start_time, zcr_end_time, zcr_type', 'required'),
			array('zcr_type, zcr_status, status, zp_id', 'numerical', 'integerOnly'=>true),
			array('zcr_title, company_name', 'length', 'max'=>100),
			array('zcr_content,zcr_condition', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zcr_id, zcr_title, zcr_content, zm_id, zp_id, zcr_start_time, zcr_end_time, zcr_type, zcr_condition, zcr_status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'discount_product'=>array(self::HAS_MANY, 'ProDiscount', 'zcr_id'),
				'product'=>array(self::BELONGS_TO, 'Product', 'zp_id'),
				'company'=>array(self::BELONGS_TO, 'Company', 'zm_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zcr_id' => 'Zcr',
			'zcr_title' => 'Zcr Title',
			'zcr_content' => 'Zcr Content',
			'zm_id' => 'Zm',
			'zp_id' => 'Zp',
			'zcr_start_time' => 'Zcr Start Time',
			'zcr_end_time' => 'Zcr End Time',
			'zcr_type' => 'Zcr Type',
			'zcr_condition' => 'Zcr Condition',
			'zcr_status' => 'Zcr Status',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zcr_id',$this->zcr_id);
		$criteria->compare('zcr_title',$this->zcr_title,true);
		$criteria->compare('zcr_content',$this->zcr_content,true);
		$criteria->compare('zm_id',$this->zm_id);
		$criteria->compare('zp_id',$this->zp_id);
		$criteria->compare('zcr_type',$this->zcr_type);
		$criteria->compare('zcr_condition',$this->zcr_condition,true);
		$criteria->compare('zcr_status',$this->zcr_status);
		
		//根据公司名称筛选
		if(!empty($this->company_name)){
			$criteria->join = ' LEFT JOIN {{Company}} c ON c.zm_id=t.zm_id ';
			$criteria->addCondition(' c.zc_name like "%'.$this->company_name.'%" ');
		}else{
			$criteria->compare('zm_id',$this->zm_id);
		}
		
		//根据状态查询
		if(!empty($this->status)){
			if($this->status==1){
				$criteria->compare('zcr_start_time','<='.time());
				$criteria->compare('zcr_end_time','>='.time());
			}elseif($this->status==2){
				$criteria->compare('zcr_start_time','>'.time());
			}elseif($this->status==3){
				$criteria->compare('zcr_end_time','<'.time());
			}
		}else{
			$criteria->compare('zcr_start_time',$this->zcr_start_time);
			$criteria->compare('zcr_end_time',$this->zcr_end_time);
		}
		
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * 获取促销类型
	 */
	public function getZcrType($type=null){
		if($type==null){
			return array(
				'1'=>'减金额',		
				'2'=>'赠送优惠券',
			//	'3'=>'赠送赠品',
			//	'4'=>'免运费',
			);
		}else{
			$level = $this->getZcrType();
			if(array_key_exists($type,$level))
				return $level[$type];
		}
	}
	
	/**
	 * 保存前的操作
	 * 生效条件系列化
	 */
	protected function beforeSave(){
		if($this->isNewRecord){
			$this->zm_id = Company::model()->getCmpUserId();
			$this->zcr_status = 1;
		}
		if(isset($this->zcr_condition['condition'])){
			foreach($this->zcr_condition['condition'] as $key=>$val){
				$zcr_condition[] = array(
						'condition'=>$val,
						'value'=>$this->zcr_condition['value'][$key],
						);
			}
			$this->zcr_condition = serialize($zcr_condition);
		}else{
			$this->zcr_condition = serialize($this->zcr_condition);
		}
		return true;
	}
	
	/**
	 * 查找后的操作
	 * 生效条件反系列化
	 */
	protected function afterFind(){
		$this->zcr_condition = unserialize($this->zcr_condition);
		return true;
	}
	
	/**
	 * 检验促销活动是否重复重突
	 */
	public function CheckPromotion($pid){
		foreach((array)$pid as $val){
			//有无正在进行中的限时折扣
			$criteria=new CDbCriteria;
			$criteria->compare('zp_id',$val);
			$criteria->compare('zpd_end_time','>'.time());//没有结束
			$criteria->compare('zpd_status',1);//状态1开启
			if(ProDiscount::model()->exists($criteria)){
				return false;
			}
			//有正在进行中的团购秒杀
			$criteria=new CDbCriteria;
			$criteria->compare('zp_id',$val);
			$criteria->compare('zpt_close',1);//是否关闭1否
			$criteria->compare('zpt_status',array(1,2));//状态1:审核中2:通过3:未通过
			$criteria->compare('zpt_end_time','>'.time());//没有结束
			if(ProTuan::model()->exists($criteria)){
				return false;
			}
		}
		return true;
	}
	
	/**
	 * 状态，用于筛选
	 */
	public function getStatus($type=null){
		if($type==null){
			return array(
					''=>'全部',
					'1'=>'正在进行',
					'2'=>'未开始',
					'3'=>'已结束',
			);
		}
	}
	
	/**
	 * 根据产品ID查找该产品正在进行中的活动
	 */
	public function getPromotion($id){
		//产品ID为空，返回空值，修正预览的情况下出错
		if(empty($id))
			return;
		//促销专区
		$criteria=new CDbCriteria;
		$criteria->compare('zp_id',$id);
		$criteria->compare('zpd_start_time','<'.time());//已经开始 
		$criteria->compare('zpd_end_time','>'.time());//还没结束
		$criteria->compare('zpd_status',1);//状态1开启
		$model = ProDiscount::model()->find($criteria);
		if(!empty($model)){
			$data['type'] = 'discount';
			$data['model'] = $model;
			return $data;
		}
		//团购,秒杀
		$criteria=new CDbCriteria;
		$criteria->compare('zp_id',$id);
		$criteria->compare('zpt_close',1);//是否关闭1否
		$criteria->compare('zpt_start_time','<='.time());//已经开始
		$criteria->compare('zpt_end_time','>'.time());//已经开始
		$criteria->compare('zpt_status',2);//状态1:审核中2:通过3:未通过
		$model = ProTuan::model()->find($criteria);
		if(!empty($model)){
			if($model->zpt_type==1){
				$data['type'] = 'tuan';//团购
			}elseif($model->zpt_type==2){
				$data['type'] = 'promotion';//限时抢购
			}
			
			$data['model'] = $model;
			return $data;
		}
		return false;
	}

	/**
	 * 计算满就送
	 */
	public function getFullsent($price = 0){
		$time = time();
		$criteria=new CDbCriteria;		
		$criteria->compare('zcr_type',1);//满就送类型
		$criteria->compare('zcr_status',1);//满就送类型
		$criteria->compare('zcr_start_time','<'.$time);//开始时间
		$criteria->compare('zcr_end_time','>'.$time);//没有结束
		$model = $this->find($criteria);
		$data = $model->zcr_condition;
		foreach ($data as $value) {
			if($price >= $value['condition']){
				$return = $value['value'];
			}

		}		
		return $return;
	}
	
}